package com.huaruan.food.biz.admin.food.imp;

import java.util.ArrayList;
import java.util.List;

import com.huaruan.food.biz.admin.food.IFoodManager;
import com.huaruan.food.biz.admin.food.IFoodTypeManager;
import com.huaruan.food.dao.IDao;
import com.huaruan.food.entity.Food;
import com.huaruan.food.entity.FoodType;
import com.huaruan.food.javabean.PageObj;

public class FoodManagerImp implements IFoodManager {
	private IDao dao;
	private IFoodTypeManager tm;
	public void setTm(IFoodTypeManager tm){
		this.tm = tm;
	}
	public void setDao(IDao dao) {
		this.dao = dao;
	}

	@Override
	public boolean isExistFoodName(String name) {
		List<Food> list = dao.findByNamedQuery("isExistFoodName", name);
		return list != null && list.size() > 0;
	}

	@Override
	public boolean addFood(Food food) {
		return dao.addObj(food);
	}

	@Override
	public PageObj<Food> findFoods(PageObj<Food> pageObj) {
		pageObj.setQueryObj(new Food());
		List<Food> result = dao.find(pageObj.getQueryObj(),
				pageObj.getCurrPage(), pageObj.getRows());
		if (null != result && result.size() > 0) {
			pageObj.setResults(result);
			pageObj.setAllRows(dao.listCount(pageObj.getQueryObj()));
		}
		return pageObj;
	}

	@Override
	public List<FoodType> getAllFoodType() {
		return tm.getAllFoodType();
	}
	
	public PageObj<Food> getFood(int currPage,int rows){
		PageObj<Food> p = new PageObj<Food>();
		p.setCurrPage(currPage);
		p.setQueryObj(new Food());
		p.setRows(rows);
		
		List<Food> result = dao.find(p.getQueryObj(),
				p.getCurrPage(), p.getRows());
		if (null != result && result.size() > 0) {
			p.setResults(result);
			p.setAllRows(dao.listCount(p.getQueryObj()));
		}
		
		return p;
	}
	@Override
	public List<Food> getAllFood() {
		PageObj<Food> p = getFood(0,0);
		return p.getResults();
	}
	@Override
	public int delFood(String [] foodIds) {
		StringBuffer hql=new StringBuffer("delete from  Food where id = ");
		for (int i = 0; i < foodIds.length; i++) { 
	       if (foodIds.length-1==i) { 
	        	hql.append(Integer.parseInt(foodIds[i]) ); 
	       } else{ 
	    	   hql.append(Integer.parseInt(foodIds[i]) + " or id="); 
	       } 
	    } 
		return dao.delOrUpdate(hql.toString());
	}
	@Override
	public List<FoodType> getAllFoodType(String type) {
		return dao.findByNamedQuery("selFoodType");
	}
	@Override
	public boolean updateFood(Food food) {
		boolean bool=false;
		try {
			dao.updateObj(food);
			bool=true;
		} catch (Exception e) {
			bool=false;
		}
		return bool;
	}
	@Override
	public List<Food> tuijian() {
		return dao.findByNamedQuery("tjOrYh", 2);
	}
	@Override
	public List<Food> youhui() {
		return dao.findByNamedQuery("tjOrYh", 3);
	}
	
	@Override
	public Food getFood(int id) {
		return dao.get(Food.class, id);
	}
	@Override
	public List<Food> findFoodsByIds(List<String> ids) {
		if(null!=ids){
			StringBuffer hql= new StringBuffer("from Food where id in (");
			for (int i = 0; i <ids.size(); i++) {
				hql.append(ids.get(i));
				if(ids.size()-1>i){
					hql.append(",");
				}
			}
			hql.append(")");
			return dao.find(hql.toString());
		}
		return null;
	}

}
